package com.aigou.admin.dao.impl;

import com.aigou.admin.dao.OrderProductDAO;
import com.aigou.admin.entity.OrderProduct;
import com.aigou.admin.util.JDBCUtils;

import java.util.List;

public class OrderProductDAOImpl implements OrderProductDAO {
    @Override
    public List<OrderProduct> getAllOrderProducts() {
        return JDBCUtils.executeQuery(OrderProduct.class, "SELECT * FROM s_order_product");
    }

    @Override
    public OrderProduct getOrderProductById(String id) {
        List<OrderProduct> orderProducts = JDBCUtils.executeQuery(OrderProduct.class, "SELECT * FROM s_order_product WHERE id=?", id);
        return orderProducts.isEmpty() ? null : orderProducts.get(0);
    }

    @Override
    public int addOrderProduct(OrderProduct orderProduct) {
        return JDBCUtils.executeUpdate("INSERT INTO s_order_product(id, order_id, product_id, product_num) VALUES(?, ?, ?, ?)",
                orderProduct.getId(),
                orderProduct.getOrder_id(),
                orderProduct.getProduct_id(),
                orderProduct.getProduct_num());
    }

    @Override
    public int deleteOrderProduct(String id) {
        return JDBCUtils.executeUpdate("DELETE FROM s_order_product WHERE id=?", id);
    }

    @Override
    public int updateOrderProduct(OrderProduct orderProduct) {
        return JDBCUtils.executeUpdate("UPDATE s_order_product SET order_id=?, product_id=?, product_num=? WHERE id=?",
                orderProduct.getOrder_id(),
                orderProduct.getProduct_id(),
                orderProduct.getProduct_num(),
                orderProduct.getId());
    }

    @Override
    public Integer selectOrderProductCount() {
        return JDBCUtils.executeCount("SELECT count(*) FROM s_order_product");
    }

    @Override
    public List<OrderProduct> selectOrderProductLimits(int start, int row) {
        return JDBCUtils.executeQuery(OrderProduct.class, "SELECT * FROM s_order_product LIMIT ?, ?", start, row);
    }

}
